<?php

/**
 *
 * @category 	SMCore
 * @package 	SMCore >> Base model
 * @copyright 	Copyright (c) 2000-2011
 * @license 	http://www.yourdomain.com
 * @version 	SMCore version 1.0.0
 * @author
 * @implement 	Name of developer
 */
class Model_AclUser extends Model_Entities_AclUser
{

    /**
     *
     * @param unknown_type $fieldName
     * @param unknown_type $fieldValue
     * @return unknown
     */
    public function getDataByFieldName($fieldName = '', $fieldValue = '')
    {
        $array = array();
        if (!empty($fieldName) && !empty($fieldValue)) {
            $array = $this->search($fieldName, $fieldValue);
            if (is_array($array) && count($array) > 0) {
                $obj = reset($array);
                $array = $obj->toArray();
            }
            return $array;
        }
    }

    /**
     * @todo insert or update data to table blog_comment
     * @param unknown_type $params
     * @param unknown_type $isNotAllowSetValueNull
     * @return unknown
     */
    public function add($params = array(), $isNotAllowSetValueNull = false)
    {
        $obj = new $this();
        $datas = $this->buildData($params);
        $obj = $this->setObject($datas);
        $id = $obj->save(array(), $isNotAllowSetValueNull);
        return $id;
    }

    /**
     * @todo This function save will accept value NULL. Because in table there are some field is value NULL
     * @param array $filter_array
     * @param unknown_type $isNotAllowNull
     */
    public function save($filterArray = array(), $isNotAllowSetValueNull = true)
    {
        return $this->getMapper()->save($this, $filterArray, $isNotAllowSetValueNull, 'id');
    }

    /**
     * @todo set value of property for object, only property don't have in $filter_array
     * @param unknown_type $datas
     * @param unknown_type $filter_array
     * @return unknown
     */
    public function setObject($datas = array(), $filterArray = array())
    {
        $obj = new $this();
        $classMethods = get_class_methods(get_class($obj));
        $funcName = '';
        if (!empty($datas['id'])) {
            //If to exist id we will excute add action
            $obj->find($datas['id']);
        }
        if (!empty($datas) && is_array($datas)) {
            foreach ($datas as $key => $value) {
                if (!empty($key) && $key !== 'id') {
                    if (!in_array($key, $filterArray)) {
                        //build function Name
                        $arr = explode("_", $key);
                        $funcName = 'set';
                        if (is_array($arr)) {
                            for ($i = 0; $i < count($arr); $i++) {
                                $funcName .= ucwords($arr[$i]);
                            }
                        }
                        if (in_array($funcName, $classMethods)) {
                            //call set function name
                            $obj->$funcName($value);
                        }
                    }
                    $funcName = '';
                }
            }
        }
        return $obj;
    }

    /**
     *
     * @param unknown_type $params
     */
    public function buildData($params = array())
    {
        $datas = array();
        $datas['id'] = !empty($params['id']) ? $params['id'] : '';
        $datas['role_id'] = !empty($params['role_id']) ? $params['role_id'] : '';
        $datas['user_id'] = !empty($params['user_id']) ? $params['user_id'] : '';
        return $datas;
    }

    /**
     * @todo validate for this contest
     * @param $data
     */
    public function validate($datas = array())
    {
        $view = new Zend_View();
        $valid = array();
        $valid['errors'] = array();
        $valid['errors'] = false;
        $valid['isError'] = false;
        $validEmpty = new Zend_Validate_NotEmpty();
        if (empty($datas['role_id'])) {
            $valid['errors']['role_id'] = "Role can not empty.";
            $valid['isError'] = true;
        }
        if (empty($datas['user_id'])) {
            $valid['errors']['user_id'] = "User can not empty.";
            $valid['isError'] = true;
        }

        return $valid;
    }

}
